import * as user from '@/api/user.js'
import api from '@/api'
const state = {
	store: {},
	cart: [],
	orderType: 'takeout',
	address: {},
	member: uni.getStorageSync('member') || {},
	order: {},
	token: uni.getStorageSync('token') || null,
	referrer: uni.getStorageSync('referrer') || "",
	userId:uni.getStorageSync('userId') || "",
	mobile:uni.getStorageSync('mobile') || "",
}
const getters = {
	isLogin: state => Object.keys(state.member).length > 0 && state.token, //是否登录
	store: state => state.store,
	address: state => state.address,
	orderType: state => state.orderType,
	member: state => state.member,
	referrer: state => state.referrer,
	userId:state => state.userId,
	mobile:state => state.mobile
}
const mutations = {
	SET_USERID(state,userId){
		uni.setStorageSync('userId',userId);
		state.userId = userId
	},
	SET_ORDER_TYPE(state, type) {
		state.orderType = type
	},
	SET_MEMBER(state, member) {
		uni.setStorageSync('member', member);
		state.member = member
	},
	SET_ADDRESS(state, address) {
		state.address = address
	},
	SET_STORE(state, store) {
		state.store = store
	},
	SET_CART(state, cart) {
		state.cart = cart
	},
	REMOVE_CART(state) {
		state.cart = []
	},
	SET_ORDER(state, order) {
		state.order = order
	},
	SET_TOKEN(state, token) {
		uni.setStorageSync('token', token);
		state.token = token;
	},
	SET_REFERRER(state, referrer) {
		uni.setStorageSync('referrer', referrer);
		state.referrer = referrer;
	},
	SET_MOBILE(state, mobile) {
		uni.setStorageSync('mobile', mobile);
		state.mobile = mobile;
	},
}
const actions = {
	//登录授权接口
	loginByWeiXin({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			user.loginByWeiXin(payload).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	// 获取店铺信息
	shopInfo({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			user.shopInfo(payload).then(res => {
				commit('SET_STORE', res.data.shop);
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//更新用户手机
	updateMobile({commit},payload={}){
		return new Promise((resolve,reject)=>{
			user.updateMobile(payload).then(res=>{
				resolve(res)
			}).catch(err=>{
				reject(err)
			})
		})
	},
	//获取用户信息
	userInfo({commit},payload={}){
		return new Promise((resolve,reject)=>{
			user.userInfo(payload).then(res=>{
				resolve(res)
			}).catch(err=>{
				reject(err)
			})
		})
	}
}

export default { state, getters, mutations, actions };
